local skel = fk.CreateSkill {
  name = "emo__zidong",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__zidong"] = "自动",
  [":emo__zidong"] = "锁定技，每当你需要使用或打出【闪】时，将手牌最左侧的基本牌当【闪】使用或打出。每当延时锦囊或其他角色使用的锦囊牌对你生效前，将手牌最左侧的锦囊牌当【无懈可击】对之使用。当你处于濒死状态时，将随机装备牌当【酒】使用。准备阶段，你获得场上的【诸葛连弩】。",

  ["$emo__zidong1"] = "Begin defense.",
  ["$emo__zidong2"] = "It dont work.",
  ["$emo__zidong3"] = "Self-rescue initiated.",
  ["$emo__zidong4"] = "Attack mode activated",
}

skel:addEffect(fk.PreCardEffect, {
  anim_type = "defensive",
  audio_index = 2,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return data.to == player and data.card and data.card.type == Card.TypeTrick
    and (data.card.sub_type == Card.SubtypeDelayedTrick or (target and target ~= player))
    and table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeTrick end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cardId = table.find(player:getCardIds("h"), function (id)
      if Fk:getCardById(id).type == Card.TypeTrick then
        local c = Fk:cloneCard("nullification")
        c.skillName = skel.name
        c:addSubcard(id)
        return not player:prohibitUse(c)
      end
    end)
    if not cardId then return end
    local card = Fk:cloneCard("nullification")
    card.skillName = skel.name
    card:addSubcard(cardId)
    local use = {from = player, card = card, toCard = data.card, responseToEvent = data}
    room:useCard(use)
  end,
})

skel:addEffect(fk.AskForCardUse, {
  anim_type = "defensive",
  audio_index = 1,
   can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or target ~= player then return false end
    return Exppattern:Parse(data.pattern):matchExp("jink")
    and table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeBasic end)
  end,
  on_use = function (self, event, target, player, data)
    local cardId = table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeBasic end)
    if not cardId then return end
    local card = Fk:cloneCard("jink")
    card.skillName = skel.name
    card:addSubcard(cardId)
    if player:prohibitUse(card) then return end
    data.result = { from = player, card = card }
    return true
  end,
})

skel:addEffect(fk.AskForCardResponse, {
  anim_type = "defensive",
  audio_index = 1,
   can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or target ~= player then return false end
    return Exppattern:Parse(data.pattern):matchExp("jink")
    and table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeBasic end)
  end,
  on_use = function (self, event, target, player, data)
    local cardId = table.find(player:getCardIds("h"), function (id) return Fk:getCardById(id).type == Card.TypeBasic end)
    if not cardId then return end
    local card = Fk:cloneCard("jink")
    card.skillName = skel.name
    card:addSubcard(cardId)
    if player:prohibitResponse(card) then return end
    data.result = { from = player, card = card }
    return true
  end,
})

skel:addEffect(fk.AskForPeaches, {
  anim_type = "defensive",
  audio_index = 3,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return data.who == player and not player:prohibitUse(Fk:cloneCard("analeptic"))
    and table.find(player:getCardIds("he"), function (id) return Fk:getCardById(id).type == Card.TypeEquip end)
  end,
  on_use = function (self, event, target, player, data)
    while player.hp < 1 and not player.dead do
      local cards = table.filter(player:getCardIds("he"), function (id)
        if Fk:getCardById(id).type == Card.TypeEquip then
          local c = Fk:cloneCard("analeptic")
          c.skillName = skel.name
          c:addSubcard(id)
          return not player:prohibitUse(c) and not player:isProhibited(player, c)
        end
      end)
      if #cards == 0 then break end
      local card = Fk:cloneCard("analeptic")
      card.skillName = skel.name
      card:addSubcard(table.random(cards))
      local use = {from = player, card = card, tos = {player}, extra_data = {analepticRecover = true}}
      player.room:useCard(use)
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  audio_index = 4,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return player.phase == Player.Start and target == player and
    table.find(player.room.alive_players, function (p)
      return table.find(p:getCardIds("e"), function (id) return Fk:getCardById(id).trueName == "crossbow" end) ~= nil
    end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      local cardId = table.find(p:getCardIds("e"), function (id)
        return Fk:getCardById(id).trueName == "crossbow"
      end)
      if cardId then
        room:obtainCard(player, cardId, true, fk.ReasonPrey, player, skel.name)
        break
      end
    end
  end,
})

return skel
